package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.MoHuApplication;
import com.wgts.wgts.control.oss.OssService;
import com.wgts.wgts.mvp.model.ISubmitTaskActivityModel;
import com.wgts.wgts.mvp.presenter.ISubmitTaskActivityPresenter;
import com.wgts.wgts.mvp.view.ISubmitTaskActivityView;
import com.wgts.wgts.netmodle.FastDFSFile;
import com.wgts.wgts.netmodle.FileManager;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.TransformUtils;
import com.wgts.wgts.rxjava.MVPResultSubscriber;

import org.csource.common.NameValuePair;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import rx.Observable;
import rx.Subscriber;

/*
 *
 *
 */public class SubmitTaskActivityPresenter implements ISubmitTaskActivityPresenter {
     private ISubmitTaskActivityView view;
     private ISubmitTaskActivityModel model;

    public SubmitTaskActivityPresenter(ISubmitTaskActivityView view, ISubmitTaskActivityModel model) {
        this.view = view;
        this.model = model;
    }


    @Override
    public void uploadImg(final String imgPath) {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(final Subscriber<? super String> subscriber) {
                //String result = submitTask(imgPath);

                OssService.getInstance(MoHuApplication.getInstance()).beginupload("task", imgPath, new OssService.ProgressCallback() {
                    @Override
                    public void onProgressCallback(double progress, String url) {
                        if(progress == 100){
                            subscriber.onNext(url);
                        }
                    }
                });

            }
        }).compose(TransformUtils.<String>defaultSchedulers()).subscribe(new MVPResultSubscriber<String>() {
            @Override
            public void netx(String aBoolean) {
                view.loadResult(aBoolean);
            }
        });
    }

    @Override
    public void issueTask(Integer taskId, String taskCode, String taskUrl) {
        model.issueTask(taskId, taskCode, taskUrl, new HttpResultSubscriber<Integer>() {
            @Override
            public void onSuccess(Integer currentEntity) {
                view.issueResult(currentEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void submitTask(Integer taskId, Integer userTaskId, String completeUrl) {
        model.submitTask(taskId, userTaskId, completeUrl, new HttpResultSubscriber<Integer>() {
            @Override
            public void onSuccess(Integer currentEntity) {
                view.submitResult(currentEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    private String submitTask(String filePath){

        String[]fileTemp = filePath.split("\\.");
        String ext = fileTemp[fileTemp.length - 1];
        FileInputStream fileReader = null;
        try {
            fileReader = new FileInputStream(filePath);
            byte[] bytes = new byte[fileReader.available()];
            int length = fileReader.read(bytes);
            final FastDFSFile file = new FastDFSFile(bytes,ext);
            final NameValuePair[] meta_list = new NameValuePair[4];
            meta_list[0] = new NameValuePair("fileName", "");
            meta_list[1] = new NameValuePair("fileLength", String.valueOf(length));
            meta_list[2] = new NameValuePair("fileExt", ext);
            meta_list[3] = new NameValuePair("fileAuthor", "");

            return FileManager.getInstance().upload(file,meta_list);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }

    }
}
